package xhwl.insurance.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import de.greenrobot.event.EventBus;
import xhwl.insurance.R;
import xhwl.insurance.customView.CircleImageView;
import xhwl.insurance.event.StopVoiceEvent;
import xhwl.insurance.http.bean.resp.ForumCommentResp;
import xhwl.insurance.ui.voicesupport.MediaPlay;
import xhwl.insurance.util.ImgTools;
import xhwl.insurance.view.ForumPictureView;

/**
 * Created by Administrator on 2017/10/9.
 * 论坛评论Adapter
 */

public class ForumCommentListAdapter extends BaseAdapter {
    Context context;
    List<ForumCommentResp> commentList;
    private String[] urls;

    public ForumCommentListAdapter(Context context, List<ForumCommentResp> commentList) {
        this.context = context;
        this.commentList = commentList;

    }


    @Override
    public int getCount() {
        return commentList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final  ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.forum_comment_item, null);
            holder.commentNickname = (TextView) convertView.findViewById(R.id.commentNickname);
            holder.commentItemTime = (TextView) convertView.findViewById(R.id.commentItemTime);
            holder.commentItemContent = (TextView) convertView.findViewById(R.id.commentItemContent);
            holder.forumPicView = (ForumPictureView) convertView.findViewById(R.id.forumPicView);
            holder.commentItemImg = (CircleImageView) convertView.findViewById(R.id.commentItemImg);
            holder.ll_voice = (LinearLayout) convertView.findViewById(R.id.ll_voice);
            holder.voice_display_voice_play = (ImageView) convertView.findViewById(R.id.voice_display_voice_play);
            holder.voice_display_voice_time = (TextView) convertView.findViewById(R.id.voice_display_voice_time);
            holder.mView = (View) convertView.findViewById(R.id.v_anime);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.commentNickname.setText(commentList.get(position).getREPLY_NAME());//发表评论人姓名
        holder.commentItemTime.setText(commentList.get(position).getREPLY_DATE());//时间
        holder.commentItemContent.setText(commentList.get(position).getREPLY_DETAILS());//内容
        ImgTools.getInstance().getImgFromNetByUrl(commentList.get(position).getREPLY_ICON(), holder.commentItemImg, R.drawable.touxiang);
        holder.forumPicView.setData(commentList.get(position).getREPLY_ANNEX(), 3);
        if (commentList.get(position).getVOICE_LONG() != null) {
            holder.voice_display_voice_time.setText(commentList.get(position).getVOICE_LONG() + "\"");
        } else {
            holder.voice_display_voice_time.setText("未知");
        }
        if (commentList.get(position).getREPLY_VOICE() != null) {
            holder.ll_voice.setVisibility(View.VISIBLE);
            final View finalConvertView = convertView;
            holder.voice_display_voice_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new StopVoiceEvent());

                    if (listItemBtnClickListener != null) {
                        listItemBtnClickListener.onClick(position, finalConvertView, parent,commentList.get(position).getREPLY_VOICE());
                    }
                }
            });

        } else {
            holder.ll_voice.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        TextView commentNickname, commentItemTime, commentItemContent, voice_display_voice_time;
        CircleImageView commentItemImg;
        ForumPictureView forumPicView;
        LinearLayout ll_voice;
        ImageView voice_display_voice_play;
        View mView;

    }
    private ListItemBtnClickListener listItemBtnClickListener = null;

    public ListItemBtnClickListener getListItemBtnClickListener() {
        return listItemBtnClickListener;
    }
    public void setListItemBtnClickListener(ListItemBtnClickListener listItemBtnClickListener) {
        this.listItemBtnClickListener = listItemBtnClickListener;
    }
    public interface ListItemBtnClickListener {
//        public void onClick(int position, View convertView, ViewGroup parent);

        void onClick(int position, View finalConvertView, ViewGroup parent, String reply_voice);
    }
}
